1864C - Divisor Chain - CodeForces Solution


bitmasks constructive algorithms math number theory

Please click on ads to support us..

C++ Code:

//                                                                                                  san
//                                                                                                narayan
//                                                                                              narayanraja
//                                                                                                narayan
//                                                            narayannarayan                        san                      narayannarayannan
//                                                                narayannarayannar                                      narayannarayannan
//                                                                     narayannarayannarayan                      narayannarayannarayan
//                                                                          narayannarayannarayan            narayannarayannarayan
//                                                                                 narayannarayannarayannarayannarayannayan
//                                                                                         narayannarayannarayann
//                                                    narayannarayannan
//                                                narayannarayannarayannarayannan
//                                            narayannarayannarayannarayannarayan
//                                        narayannarayannarayannarayannarayan
//                                    narayannarayannarayannarayannarayannarayan
//                                 narayannarayannan                    narayan
//                                                                       narayan
//                                                                       narayan
//                                                                      narayan                            narayannarayannan
//                                                                     narayan                        narayannarayannarayannarayan
//                                                                    narayan                     narayannarayannarayannarayannarayannarayannarayannan
//                                                                   narayan                  narayannarayannarayannarayannarayannarayannarayannan
//                                                                  narayan                 narayannarayannarayan                  narayannarayannan
//                                                                narayan                 narayannarayannan                         narayannarayannan
//                                                              narayan                narayannarayannan                                narayannarayann
//                                                           narayannan              narayannarayannarayann                                    narayannarayann
//                                                        narayannar              narayannarayannarayan                                        narayannarayann
//                                                     narayannarayan              narayannarayannarayan                                           narayannayan
//                                                  narayannarayannarayannarayannarayannarayannarayan                                              narayannar
//                                                  narayannarayannarayannarayannarayannarayannarayan                                                 narayan
//                                                     narayannayannarayannarayannarayannarayannarayan                                                    narayan
//                                                       narayannarnarayannarayannarayannarayan                                                      narayan
//                                                          narayannannarayannarayannarayannan                                                          narayan
//                                                             narayan                                                                            narayannarayan
//                                                               narayan                     nar                                                 narayannarayan
//                                                                 narayan                   nar                                                narayannarayan
//                                                                   narayan                 nar                                               narayannarayan
//                                                                      narayan              narayan                                             narayannarayan
//                                                                        narayan             narayann                                          narayannarayan
// narayan                                                                 narayan             narayan                                        narayannarayan
//  narayan                                                                  narayan            narayan                                      narayannarayan
//   narayan                                                                narayan             narayan                                   narayannarayan
//     narayannan                                                              narayan             narayan                                 narayannarayan
//       narayannan                                                             narayan              narayannan                            narayannarayan
//         narayannan                                                          narayan                 narayannarayan                      narayannarayan
//           narayannan                                                       narayan                    narayannarayan                narayannarayan
//              narayannan                                                  narayan                        narayannarayannarayannarayannarayannarayannarayannarayannayan
//                 narayannarayan                                          narayannan                            narayannarayannarayannarayannarayannarayannarayannarayannan
//                    narayannarayan                                   narayannan                                 narayannarayannarayannarayannan
//                       narayannarayannan                            narayannan                                       narayannarayannarayannan
//                          narayannarayannayan                   narayannarayan
//                              narayannarayannarayannarayannayannarayannarayann
//                                  narayannarayannarayannarayannarayannayan
//                                      narayannarayannarayannayan
//                                           narayannarayannan
//
//
// ॐ भूर्भुव: स्व: तत्सवितुर्वरेण्यं भर्गो देवस्य धीमहि धियो यो न: प्रचोदयात्।
// ॐ त्र्यम्बकं यजामहे सुगन्धिं पुष्टिवर्धनम्। उर्वारुकमिव बन्धनान् मृत्योर्मुक्षीय मामृतात्॥
/*
    कर्मण्येवाधिकारस्ते मा फलेषु कदाचन ।
    मा कर्मफलहेतुर्भुर्मा ते संगोऽस्त्वकर्मणि ॥
अर्थ:- तेरा कर्म करने में अधिकार है इनके फलो में नही.
     तू कर्म के फल प्रति असक्त न हो या कर्म न करने के प्रति प्रेरित न हो.
*/
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin >> t;
    while (t--)
    {

        ll n;
        cin >> n;
        // ll val=1;

        ll temp = n;

        vector<ll> bits;

        while (temp != 0)
        {
            bits.push_back(temp % 2);
            temp /= 2;
        }

        ll cnt = bits.size();

        vector<ll> ans;
        ll val = 1;

        ans.push_back(n);
        for (ll i = 0; i < cnt - 1; i++)
        {
            if (bits[i] == 1)
            {
                ans.push_back(n - val);
                n -= val;
            }
            val *= 2;
        }

        while (n != 1)
        {
            ans.push_back(n / 2);
            n /= 2;
        }

        cout << ans.size() << endl;
        for (auto i : ans)
        {
            cout << i << " ";
        }

        cout << endl;
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String